package com.hangxiang.approval.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hangxiang.approval.common.IntegerCommon;
import com.hangxiang.approval.common.RoleMenuInsertCommon;
import com.hangxiang.approval.common.RoleMenuNewCommon;
import com.hangxiang.approval.common.RoleMenuUpdateCommon;
import com.hangxiang.approval.dao.RoleMenuDao;
import com.hangxiang.approval.entity.RoleMenu;
import com.hangxiang.approval.service.RoleMenuService;
import com.hangxiang.approval.vo.FirstMenuVo;
import com.hangxiang.approval.vo.RoleMenuVo;
import com.hangxiang.approval.vo.SecondMenuVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * (RoleMenu)表服务实现类
 *
 * @author makejava
 * @since 2020-11-26 14:01:41
 */
@Service("roleMenuService")
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuDao, RoleMenu> implements RoleMenuService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private RoleMenuDao roleMenuDao;

    @Resource
    private RoleMenuService roleMenuService;

    /**
     * 通过ID查询单条数据
     *
     * @param common 主键
     * @return 实例对象
     */
    @Override
    public RoleMenuVo queryById(IntegerCommon common) {
        RoleMenuVo vo = new RoleMenuVo();
        RoleMenu roleMenu = roleMenuDao.selectById(common.getValue());
        BeanUtils.copyProperties(roleMenu, vo);
        return vo;
    }


    /**
     * 新增数据
     *
     * @param roleMenuInsertCommon 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Integer insertRoleMenu(RoleMenuInsertCommon roleMenuInsertCommon) {
        RoleMenu roleMenu = new RoleMenu();
        BeanUtils.copyProperties(roleMenuInsertCommon, roleMenu);
        roleMenuDao.insert(roleMenu);
        return roleMenu.getId();
    }





    /**
     * 修改数据
     *
     * @param roleMenuUpdateCommon 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Integer updateRoleMenu(RoleMenuUpdateCommon roleMenuUpdateCommon) {
        RoleMenu roleMenu = new RoleMenu();
        BeanUtils.copyProperties(roleMenuUpdateCommon, roleMenu);
        roleMenuDao.updateById(roleMenu);
        return roleMenu.getId();
    }

    @Override
    public void insertRoleMenuList(RoleMenuNewCommon roleMenuNewCommon) {
        // 返回一级菜单
        List<FirstMenuVo> result = roleMenuNewCommon.getFirstMenuList().stream().map(menuVo -> {
            FirstMenuVo vo = new FirstMenuVo();
            vo.setId(menuVo.getId());
            vo.setFirstName(menuVo.getFirstName());
            vo.setSecondMenuVoList(menuVo.getSecondMenuVoList());
            vo.setMenuType(menuVo.getMenuType());
            return vo;
        }).collect(Collectors.toList());
        // 获取一级菜单对应二级菜单信息
        List<RoleMenu> roleMenus = new ArrayList<>();

        for (FirstMenuVo firstMenuVo : result) {
            if(firstMenuVo.getSecondMenuVoList()!=null&&firstMenuVo.getSecondMenuVoList().size()!=0) {
                for (SecondMenuVo secondMenuVo : firstMenuVo.getSecondMenuVoList()) {
                    RoleMenu roleMenu1 = new RoleMenu();
                    roleMenu1.setRoleId(roleMenuNewCommon.getUserId());
                    roleMenu1.setFirstId(firstMenuVo.getId());
                    roleMenu1.setFirstName(firstMenuVo.getFirstName());
                    roleMenu1.setSecondId(secondMenuVo.getId());
                    roleMenu1.setSecondName(secondMenuVo.getSecondName());
                    roleMenu1.setMenuType(firstMenuVo.getMenuType());
                    roleMenu1.setCreateTime(new Date());
                    roleMenus.add(roleMenu1);
                }
            }else {
                RoleMenu roleMenu1 = new RoleMenu();
                roleMenu1.setRoleId(roleMenuNewCommon.getUserId());
                roleMenu1.setFirstId(firstMenuVo.getId());
                roleMenu1.setFirstName(firstMenuVo.getFirstName());
                roleMenu1.setMenuType(firstMenuVo.getMenuType());
                roleMenu1.setCreateTime(new Date());
                roleMenus.add(roleMenu1);
            }
        }
        roleMenuService.saveBatch(roleMenus);
    }
}